using Dream.Dialogue.Implement;
using Light;
using UnityEngine;

namespace Dream
{
    /// <summary>
    ///     检测玩家并触发Talk
    /// </summary>
    public class PlayerDetect : MonoBehaviour
    {
        public bool isTouchingPlayer;

        public InteractTalk _talk;

        public GameObject panel;

        /// <summary>
        ///     关卡启动器
        /// </summary>
        public LevelStarter _levelStarter;

        public IPlayerDetecedTrigger[] _triggers;

        private void Awake()
        {
            _talk = GetComponent<InteractTalk>();

            //设置在对话结束后开始加载游戏场景
            _talk.SetTextCompleteCallBack(OnTalkEndCallBack);

            _triggers = GetComponents<IPlayerDetecedTrigger>();
        }


        private void Update()
        {
            if (isTouchingPlayer && Input.GetKeyDown(KeyCode.F)) _talk.StartTalk();
        }


        private void OnTriggerEnter2D(Collider2D other)

        {
            if (other.CompareTag("Player")) isTouchingPlayer = true;
        }

        private void OnTriggerExit2D(Collider2D other)
        {
            if (other.CompareTag("Player")) isTouchingPlayer = false;
        }

        /// <summary>
        ///     说话结束调用的效果
        /// </summary>
        private void OnTalkEndCallBack()
        {
            foreach (var trigger in _triggers) trigger.Trigger();
            panel.SetActive(false);
            AudioManager.Instance.PauseBGM();
        }
    }
}